﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommonUtility;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Model.DTO.Manager;
using Models.DTO.Api;
using Newtonsoft.Json;
using WPay.Domain;
using WPay.Domain.Model.DTOs;
using WPay.Infrastructure.Unity;

namespace WPay.Web.API.Controllers
{
    /// <summary>
    /// api基类
    /// </summary>
    //[Route("api/{version}/[controller]")]
    [Route("api/v{version:ApiVersion}/client/[action]")]
    [ApiController]
    [ApiVersion("1.0")]
    [EnableCors("AllowCors")]//启用跨域
    public class BaseController : ControllerBase
    {
        public LoginUserDTO LoginUser { get; set; }
        public BaseController()
        {
            LoginUser = Function.InitSession(HttpContext);
        }
        public RespDto Do<T>(Action<T, RespDto> runMethod)
        {
            var result = new RespDto();
            try
            {
                var dic = new Dictionary<string, object>();
                var keys = HttpContext.Request.Form.Keys;
                foreach (var item in keys)
                {
                    dic.Add(item, HttpContext.Request.Form[item].Contains("null") ? null : HttpContext.Request.Form[item].ToString());
                }
                var parm = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(dic));
                #region 动态属性赋值
                Type parmtyp = typeof(T);
                if (parmtyp.GetProperty("Parms") != null)
                {
                    object o = parmtyp.GetProperty("Parms").GetValue(parm, null);
                    string Value = Convert.ToString(o);
                    object v = Convert.ChangeType(JsonConvert.DeserializeObject<PageParmDTO>(Value), parmtyp.GetProperty("PageParm").PropertyType);
                    parmtyp.GetProperty("PageParm").SetValue(parm, v, null);
                }
                #endregion
                runMethod((T)parm, result);
                result.ErrorCode = 200;
            }
            catch (Exception e)
            {
                if (e.GetBaseException().GetType() == typeof(UNException))
                {
                    var custexception = ((UNException)e.GetBaseException());
                    result.ErrorCode = -1;
                    result.Message = custexception.Message ?? custexception.Message;
                    result.Success = false;
                    result.Data = default;
                    LoggerHelper.Log(result.Message + e);
                }
                else
                {
                    result.ErrorCode = -1;
                    result.Message = "系统错误,请联系管理员";
                    result.Success = false;
                    result.Data = default;
                    LoggerHelper.Log(e.Message + e.InnerException?.Message + e);
                }
            }
            return result;
        }
        public T GetRequestParms<T>()
        {
            var dic = new Dictionary<string, object>();
            var keys = HttpContext.Request.Query.Keys;
            foreach (var item in keys)
            {
                dic.Add(item, HttpContext.Request.Query[item].Contains("null") ? null : HttpContext.Request.Query[item].ToString());
            }
            var parm = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(dic));
            #region 动态属性赋值
            Type parmtyp = typeof(T);
            if (parmtyp.GetProperty("Parms") != null)
            {
                object o = parmtyp.GetProperty("Parms").GetValue(parm, null);
                string Value = Convert.ToString(o);
                object v = Convert.ChangeType(JsonConvert.DeserializeObject<PageParmDTO>(Value), parmtyp.GetProperty("PageParm").PropertyType);
                parmtyp.GetProperty("PageParm").SetValue(parm, v, null);
            }
            #endregion
            return (T)parm;
        }
        public dynamic GetRowToGrid<T>(EntityList<T> data) where T : new()
        {
            return new
            {
                page = data.Page,
                total = data.TotalPage,
                records = data.Total,
                rows = data.Data
            };
        }

    }
}